﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Web;
using System.Xml.Serialization;

using Microsoft.MSE.Catalog.Module;
using Microsoft.MSE.Catalog.Contracts;
using Microsoft.MSE.Catalog.Services;

namespace Microsoft.MSE.Catalog.ConsoleHost
{
    static class Program
    {
        static void Main(string[] args)
        {
            Console.Title = SC.ComponentName + " Console";
            try
            {
                TraceHelper.InitializeTraceSource("mseTraceSrc");
                ConsoleTraceListener ctl = new ConsoleTraceListener(true);
                ctl.Writer = Console.Out;
                TraceHelper.MseTraceSource.Listeners.Add(ctl);
                TraceHelper.TraceInformation("Starting " + SC.ComponentName + "...");
                Trace.AutoFlush = true;

                #region StartServiceCompletedEventHandler
                WCFCatalogSvcHost.StartServiceCompleted+=delegate(object sender, StartServiceCompletedEventArgs e){
                    if (e.Error == null) return;

                    if (e.Error is ApplicationException)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Out.WriteLine(e.Error.Message);
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.BackgroundColor = ConsoleColor.DarkBlue;
                        Console.Out.WriteLine(e.Error);
                        Console.ResetColor();
                    }
                };
                #endregion

                WCFCatalogSvcHost.StartServiceAsync();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.BackgroundColor = ConsoleColor.DarkBlue;
                Console.Out.WriteLine(ex);
                Console.ResetColor();
            }
            finally
            {
                Console.Out.WriteLine("Press Enter to Exit");
                Console.In.ReadLine();
                WCFCatalogSvcHost.StopService();
            }
        }

        private static void DumpConfig()
        {
            CatalogSvcConfig cfg = new CatalogSvcConfig();
            cfg.baseAddress = new Uri("http://localhost:8080/ServiceCatalog");
            cfg.DBConnString = "dbconn";
            ServiceEndpointElement svcEndpoint = new ServiceEndpointElement();
            svcEndpoint.address = new Uri("http://localhost:8080/ServiceCatalog/Basic");
            svcEndpoint.bindingXml.InnerXml = "<binding/>";
            cfg.serviceEndpoints = new ServiceEndpointElement[] { svcEndpoint };

            DataContractSerializer ser = new DataContractSerializer(typeof(CatalogSvcConfig));

            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlTextWriter.Create(sb);
            ser.WriteObject(writer, cfg);
            writer.Flush();

            //Configuration cfgStore = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            Console.WriteLine(sb);

        }

        private static void DumpDBEventNotification()
        {
            DBEventContext msg = new DBEventContext();
            msg.Event = DBEvents.EndpointsUpdated;
            DataContractSerializer ser = new DataContractSerializer(typeof(DBEventContext));

            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlTextWriter.Create(sb);
            ser.WriteObject(writer, msg);
            writer.Flush();

            Console.WriteLine(sb);
        }

        private static void DumpAcceptEvent()
        {
            AcceptEventMsg msg = new AcceptEventMsg();
            msg.AcceptEvent = new DBEventContext();
            msg.AcceptEvent.Event = DBEvents.EndpointsUpdated;

            XmlSerializer ser = new XmlSerializer(typeof(AcceptEventMsg));

            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlTextWriter.Create(sb);
            ser.Serialize(writer, msg);
            writer.Flush();

            Console.WriteLine(sb);
        }

    }
}